<?php

class Helper{
    /**
     * @param string $var Variable that will be debuged
     * @param bool $flag Specifies if the program must be stoped after of the debug
     */
    public static function debug($var, $flag = false){
        echo "<pre>"; print_r($var); echo "</pre>";
        if($flag){
            exit;
        }
    }
    
    /**
     * @param float $num Number that will be converted into monetary value
     * @return float The monetary value
     */
    public static function numberToCoin($num){
        return number_format($num, 2, ",", ".");
    }
    
    /**
     * @param float $coin Monetary value that will be converted into number
     * @return float The number
     */
    public static function coinToNumber($coin){
        $coin = str_replace(".", "", $coin);
        $coin = str_replace(",", ".", $coin);
        return $coin;
    }
    
    /**
     * @param string $date Date in DB format
     * @return string Date in PT-BR format
     */
    public static function dateDbToPtbr($date){
        return implode("/", array_reverse(explode("-", $date)));
    }
    
    /**
     * @param string $date Date in PT-BR format
     * @return string Date in DB format
     */
    public static function datePtbrToDb($date){
        return implode("-", array_reverse(explode("/", $date)));
    }
    
    /**
     * @param string $datetime Datetime that will be converted into timestamp
     * @return string The timestamp
     */
    public static function datetimeToTimestamp($datetime){
        $datetime = explode(" ", $datetime);
        $datetime[0] = explode("-", $datetime[0]);
        $datetime[1] = explode(":", $datetime[1]);
        $hora = $datetime[1][0];
        $minuto = $datetime[1][1];
        $segundo = $datetime[1][2];
        $mes = $datetime[0][1];
        $dia = $datetime[0][2];
        $ano = $datetime[0][0];
        return mktime($hora, $minuto, $segundo, $mes, $dia, $ano);
    }
    
    /**
     * @param string $date Date that will be converted into timestamp
     * @return string The timestamp
     */
    public static function dateToTimestamp($date){
        $date = explode("-", $date);
        return mktime($date[1], $date[2], $date[0]);
    }
    
    /**
     * @param string $timestamp The timestamp that will be converted into datetime
     * @return string The datetime
     */
    public static function timestampToDatetime($timestamp){
        return date("Y-m-d H:i:s", $timestamp);
    }
    
    /**
     * @param string $timestamp The timestamp that will be converted into date
     * @return sting The date
     */
    public static function timestampToDate($timestamp){
        return date("Y-m-d", $timestamp);
    }
    
    /**
     * @return string The current datetime
     */
    public static function now(){
        return date("Y-m-d H:i:s");
    }
    
    /**
     * @param string $config Array with attributes of the input, using key and value
     * @return string The input
     */
    public static function input($config){
        if(!isset($config["id"]) && isset($config["name"])){
            $config["id"] = $config["name"];
        }elseif(isset($config["id"]) && !isset($config["name"])){
            $config["name"] = $config["id"];
        }elseif(!isset($config["id"]) && !isset($config["name"])){
            return "Informe o name e/ou o id do input";
        }else{
            $input = "<input ";
            foreach($config as $key => $value){
                echo "$key=\"$value\"<br />";
                $input .= "$key=\"$value\" ";
            }
            $input .= "/>";
            return $input;
        }
    }
}